/**
 * Created by vincent on 2017/10/13.
 */
moduleStudyMap.controller("SetupEventController", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                            EnumService, ConstantService, StudyMapEventRuleResource, EventResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();

        $scope.rule = {};
    });

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.queryEvent = function () {
        //LoadingService.show();
        EventResource.findByKey({}, {keyword: $scope.eventKey, type: $scope.eventType}, function (eventResult) {
            $log.debug("勋章匹配结果", eventResult);
            $scope.eventResult = eventResult;
            //LoadingService.hide();
        });
    };

    $scope.setCurrentEvent = function (event) {
        if (!$scope.currentGroupEventRule) {
            $scope.currentGroupEventRule = {event: event};
            //$scope.currentGroupEventRule.event.id = event.id;
        } else {
            $scope.currentGroupEventRule.event = event;
            //$scope.currentGroupEventRule.event.id = event.id;
        }
    };

    $scope.unActive = function () {
        Alert.confirm("是否确认要执行该操作？", function () {
            //LoadingService.show();
            StudyMapEventRuleResource.unActive({}, {id: $scope.currentGroupEventRule.id}, function () {
                //$log.debug("勋章匹配结果",badgeResult);
                //$scope.badgeResult = badgeResult;
                //LoadingService.hide();
                Alert.success("操作成功！");
                DialogService.close();
            }, function () {
                //LoadingService.hide();
            });
        });
    }

    $scope.submit = function () {
        if (!StringUtils.isNlOrUndOrEmpty($scope.currentGroupEventRule.event)) {
            //LoadingService.show();
            if (StringUtils.isNlOrUndOrEmpty($scope.currentGroupEventRule.id)) {
                StudyMapEventRuleResource.add({},
                    {
                        eventId: $scope.currentGroupEventRule.event.id,
                        studyMapId: $scope.currentGroup.studyMap.id,
                        studyClassGroupId: $scope.currentGroup.id,
                        startTime: $scope.rule.startTime,
                        endTime: $scope.rule.endTime
                    }, function (eventRuleResult) {
                        $log.debug("章节勋章新增结果", eventRuleResult);
                        DialogService.close();
                        //LoadingService.hide();
                        Alert.success("设置勋章成功！");
                    }, function () {
                        //LoadingService.hide();
                    });
            } else {
                StudyMapEventRuleResource.update({},
                    {
                        id: $scope.currentGroupEventRule.id,
                        eventId: $scope.currentGroupEventRule.event.id,
                        studyMapId: $scope.currentGroup.studyMap.id,
                        studyClassGroupId: $scope.currentGroup.id,
                        startTime: $scope.rule.startTime,
                        endTime: $scope.rule.endTime
                    }, function (eventRuleResult) {
                        $log.debug("章节活动更新结果", eventRuleResult);
                        DialogService.close();
                        //LoadingService.hide();
                        Alert.success("设置活动成功！");
                    }, function () {
                        //LoadingService.hide();
                    });
            }
        } else {
            Alert.error("活动信息为空，无法保存！");
        }
    };

});